package com.spzx.product.api.factory;

import com.spzx.common.core.domain.R;
import com.spzx.product.api.RemoteProductSkuService;
import com.spzx.product.api.domain.dto.ProductInfoDto;
import com.spzx.product.api.domain.dto.ProductSkuDto;
import com.spzx.product.api.domain.param.SkuQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 商品sku降级处理
 *
 * @author gaozz
 */
@Component
public class RemoteProductSkuFallbackFactory implements FallbackFactory<RemoteProductSkuService> {

    private static final Logger log = LoggerFactory.getLogger(RemoteProductSkuFallbackFactory.class);

    @Override
    public RemoteProductSkuService create(Throwable throwable) {
        log.error("商品sku调用失败:{}", throwable.getMessage());
        return new RemoteProductSkuService() {
            @Override
            public R<List<ProductSkuDto>> findProductSkuNo20(String source) {
                return R.fail("获取畅销商品列表失败:" + throwable.getMessage());
            }

            @Override
            public R<List<ProductSkuDto>> skuList(Integer pageNum, Integer pageSize, SkuQuery query, String source) {
                return R.fail("获取指定商品列表失败:" + throwable.getMessage());
            }

            @Override
            public R<ProductInfoDto> getProductInfoBySkuId(Long skuId, String source) {
                return R.fail("根据skuId查询商品详情信息失败:" + throwable.getMessage());
            }

            @Override
            public R<ProductSkuDto> getSkuInfoBySkuId(Long skuId, String source) {
                return R.fail("根据skuId查询商品sku信息失败:" + throwable.getMessage());
            }

            @Override
            public R<Map<Long, BigDecimal>> getSkuPrice(Set<String> skuIds, String source) {
                return R.fail("根据skuId集合查询商品sku的价格失败:" + throwable.getMessage());
            }
        };
    }
}
